package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.GameFireBean;
import com.zucai365.football.dao.GameFireDao;
import com.zucai365.football.service.GameFireService;

@Service("gameFireService")
public class GameFireServiceImpl extends WcjServiceImpl<GameFireBean> implements GameFireService{
	
	@Autowired
	@Qualifier("gameFireDao")
	private GameFireDao gameFireDao;

	public GameFireDao getGameFireDao() {
		return gameFireDao;
	}

	public void setGameFireDao( GameFireDao gameFireDao ) {
		this.gameFireDao = gameFireDao;
	}

	@Override
	public void saveGameFire( List<GameFireBean> gameFireList ) {
		
		List<GameFireBean> gfList = new ArrayList<GameFireBean>();
		
		for( int i=0;i<gameFireList.size(); i++){
			GameFireBean gameFire = gameFireList.get(i);
			GameFireBean gf = getGameFire(gameFire);
			if( gf == null ){
				gfList.add( gameFire );
			}
		}
		
		this.gameFireDao.saveAll( gfList );
		
	}

	@Override
	public GameFireBean getGameFire( GameFireBean gameFire ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( GameFireBean.class, "gameFire" );
		criteria.createAlias( "gameFire.teamDz", "teamDz" );
		criteria.createAlias( "gameFire.footballPlayer", "footballPlayer" );
		
		
		criteria.add( Restrictions.eq( "gameFire.liansaiYearId", gameFire.getLiansaiYearId() ) );
		criteria.add( Restrictions.eq( "gameFire.liansaiId", gameFire.getLiansaiId() ) );
		criteria.add( Restrictions.eq( "teamDz.id", gameFire.getTeamDz().getId() ) );
		criteria.add( Restrictions.eq( "gameFire.lunci", gameFire.getTeamDz().getLunci() ) );
		criteria.add( Restrictions.eq( "footballPlayer.id", gameFire.getFootballPlayer().getId() ) );
		criteria.add( Restrictions.eq( "gameFire.event", gameFire.getEvent() ) );
		criteria.add( Restrictions.eq( "gameFire.eventTime", gameFire.getEventTime() ) );
		
		List<GameFireBean> list = this.gameFireDao.findByCriteria(criteria);
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
	}

	@Override
	public List<GameFireBean> searchGameFire(Long teamDzId, Long footballTeamId) {

		DetachedCriteria criteria = DetachedCriteria.forClass( GameFireBean.class, "gameFire" );
		criteria.createAlias( "gameFire.teamDz", "teamDz" );
		criteria.createAlias( "gameFire.footballPlayer", "footballPlayer" );
		
		criteria.add( Restrictions.eq( "teamDz.id", teamDzId ) );
		if( footballTeamId != null ){
			criteria.createAlias( "gameFire.eventFootballTeam", "footballTeam" );
			criteria.add( Restrictions.eq( "footballTeam.id", footballTeamId ) );	
		}
		
		criteria.addOrder( Order.desc( "footballTeam.id" ) );
		criteria.addOrder( Order.asc( "gameFire.eventTime" ) );
		
		return this.gameFireDao.findByCriteria(criteria);
		
	}

	@Override
	public List<GameFireBean> searchAllJinQiu( Long lianSaiId, Long lianSaiYearId ){
		
		DetachedCriteria criteria = DetachedCriteria.forClass( GameFireBean.class, "gameFire" );
		criteria.add( Restrictions.eq( "gameFire.liansaiYearId", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "gameFire.liansaiId", lianSaiId ) );
		
		String[] event = {"进球","点球","乌龙球"};
		criteria.add( Restrictions.in( "gameFire.event", event ) );
		
		criteria.addOrder( Order.asc( "gameFire.lunci" ) );
		
		return this.gameFireDao.findByCriteria(criteria);
		
	}
	
}
